package com.book.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.common.entity.QueryRequest;
import com.book.management.entity.Revocation;
import com.book.management.mapper.RevocationMapper;
import com.book.management.service.RevocationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 何胤宏
 * @Description:  20173969
 */

@Service
public class RevocationServiceImpl extends ServiceImpl<RevocationMapper, Revocation> implements RevocationService {
    @Autowired
    RevocationMapper revocationMapper;

    @Override
    public List<Revocation> RevocationList(Revocation revocation) {
        return this.revocationMapper.selectList(null);
    }

    @Override
    public void addRevocation(Revocation revocation) {
        this.save(revocation);
    }

    @Override
    public void updateRevocation(Revocation revocation) {
        this.saveOrUpdate(revocation);
    }

    @Override
    public IPage<Revocation> RevocationPage(QueryRequest request, Revocation revocation) {
        QueryWrapper<Revocation> revocationQueryWrapper = new QueryWrapper<>();
        if (revocation.getYear() != null) {
            revocationQueryWrapper.like("year",revocation.getYear());
        }
        if (StringUtils.isNotEmpty(revocation.getBookname())) {
            revocationQueryWrapper.like("bookname", revocation.getBookname());
        }
        Page<Revocation> revocationPage = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(revocationPage, revocationQueryWrapper);
    }
}
